//
//  MBSpaceAlbumHeaderView.m
//  MiBaoTu
//
//  Created by admin on 16/9/22.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceAlbumHeaderView.h"

@interface MBSpaceAlbumHeaderView () {
    UIView              *_bgView;
    UIView              *_contentView;
    UIImageView         *_userImgView;
    UILabel             *_nickNameLb;
    UILabel             *_musicTypeLb;
    UILabel             *_musicSizeLb;
    UIButton            *_contentBt;
    UILabel             *_contentLb;
    UIView              *_lineView;
    
    UIView              *_btView;
}

@end
@implementation MBSpaceAlbumHeaderView

- (id)init
{
    self = [super init];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor clearColor];
    [self addSubview:_bgView];
    
    _contentView = [[UIView alloc]initWithFrame:CGRectZero];
    _contentView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_contentView];
    
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImgView.backgroundColor = [UIColor clearColor];
    [_contentView addSubview:_userImgView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:14.f];
    [_contentView addSubview:_nickNameLb];
    
    //类型
    _musicTypeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _musicTypeLb.backgroundColor = [UIColor clearColor];
    _musicTypeLb.textAlignment = NSTextAlignmentLeft;
    _musicTypeLb.textColor = UIColorFromHex(0x9999999);
    _musicTypeLb.font = [UIFont systemFontOfSize:11.f];
    [_contentView addSubview:_musicTypeLb];
    
    //大小
    _musicSizeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _musicSizeLb.backgroundColor = [UIColor clearColor];
    _musicSizeLb.textAlignment = NSTextAlignmentLeft;
    _musicSizeLb.textColor = UIColorFromHex(0x9999999);
    _musicSizeLb.font = [UIFont systemFontOfSize:11.f];
    [_contentView addSubview:_musicSizeLb];
    
    //内容
    _contentLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _contentLb.backgroundColor = [UIColor clearColor];
    _contentLb.textAlignment = NSTextAlignmentLeft;
    _contentLb.textColor = UIColorFromHex(0x999999);
    _contentLb.font = [UIFont systemFontOfSize:11.f];
    _contentLb.numberOfLines = 1;
    [_contentView addSubview:_contentLb];
    
    _contentBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _contentBt.backgroundColor = [UIColor clearColor];
    [_contentBt setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    [_contentBt setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateSelected];
    [_contentBt addTarget:self action:@selector(onUpdateUIForTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_contentBt];
    
    //线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.35];
    [_contentView addSubview:_lineView];
    
    //按钮区
    _btView = [[UIView alloc]initWithFrame:CGRectZero];
    _btView.backgroundColor = [UIColor clearColor];
    [_contentView addSubview:_btView];
    
    for (int i=0; i<3; i++) {
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.backgroundColor = [UIColor whiteColor];
        [bt setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateNormal];
        [bt setTitleColor:UIColorFromHex(0x999999) forState:UIControlStateDisabled];
        bt.layer.cornerRadius = 6.f;
        bt.layer.masksToBounds = YES;
        bt.layer.borderColor = UIColorFromHex(0x999999).CGColor;
        bt.layer.borderWidth = 1;
        bt.tag = 1000+i;
        bt.titleLabel.font = [UIFont systemFontOfSize:13.];
        [bt addTarget:self action:@selector(onTapForMusicAction:) forControlEvents:UIControlEventTouchUpInside];
        [_btView addSubview:bt];
        
        if (i == 0) {
            [bt setTitle:@"播放全部" forState:UIControlStateNormal];
        }
        else if (i == 1) {
            [bt setTitle:@"全部下载" forState:UIControlStateNormal];
        }
        else if (i == 2) {
            [bt setTitle:@"更多" forState:UIControlStateNormal];
        }
    }

}

#pragma mark - 按钮 播放全部、全部下载、更多
- (void)onTapForMusicAction:(UIButton *)bt
{
    switch (bt.tag - 1000) {
        case 0:
        {
            if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemforPlayALl)]) {
                [_delegate onSelectItemforPlayALl];
            }
        }
            break;
        case 1:
        {
            if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemforDownAll)]) {
                [_delegate onSelectItemforDownAll];
            }
        }
            break;
        case 2:
        {
            if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemforMoreAll)]) {
                [_delegate onSelectItemforMoreAll];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 更新UI
- (void)onUpdateUIForTableHeaderView:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemforUpdateUI)]) {
        _contentBt.selected = !_contentBt.selected;
        [_delegate onSelectItemforUpdateUI];
    }
}

- (CGFloat)getHeightForNewUI
{
    if (_contentBt.selected) {
        //下啦
        float musicHeight = [_contentLb.text boundingRectWithSize:CGSizeMake(_contentLb.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_contentLb.font} context:NULL].size.height+4;
        return (musicHeight > 20 ? (musicHeight-20) : 0);
    } else {
        return 0;
    }
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setDateDic:(NSDictionary *)dateDic
{
    if (!_dateDic) {
        _dateDic = [[NSDictionary alloc]init];
    }
    _dateDic = dateDic;
    [self setNeedsLayout];
}

- (void)setMusicCount:(NSString *)musicCount
{
    _musicCount = musicCount;
    [self setNeedsLayout];
}

- (void)setMusicSize:(NSString *)musicSize
{
    _musicSize = musicSize;
    [self setNeedsLayout];
}

//判断下载按钮是否可点击
- (void)onUpdateUIForDonwBt:(BOOL)isenable
{
    UIButton *downBt = (UIButton *)[self viewWithTag:1001];
    downBt.enabled = isenable;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //value
    if (!_dateDic) return;
    if ([_dateDic.allKeys containsObject:@"IconPath"]) {
        [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[_dateDic objectForKey:@"IconPath"]] ] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
        _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"AlbumName"]];
        _musicTypeLb.text = [NSString stringWithFormat:@"共%@首",_musicCount];
        _musicSizeLb.text = [NSString stringWithFormat:@"%@M",_musicSize];
        _contentLb.text = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"Synopsis"]];
    }
    else if ([_dateDic.allKeys containsObject:@"FaceImg"]) {
        [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[_dateDic objectForKey:@"FaceImg"]] ] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
        if ([_dateDic.allKeys containsObject:@"Nickname"]) {
            _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"Nickname"]];
        }
        else if ([_dateDic.allKeys containsObject:@"NickName"]) {
            _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"NickName"]];
        }
        _musicTypeLb.text = [NSString stringWithFormat:@"共%@首",_musicCount];
        _musicSizeLb.text = [NSString stringWithFormat:@"%@M",_musicSize];
        _contentLb.text = [NSString stringWithFormat:@"%@",[_dateDic objectForKey:@"Signature"]];
    }
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 275*PERCEN);
    _contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 255*PERCEN);
    
    _userImgView.frame = CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 20*PERCEN, 110*PERCEN, 110*PERCEN);
    _userImgView.layer.cornerRadius = _userImgView.height/2.;
    _userImgView.layer.masksToBounds = YES;
    
    _nickNameLb.frame = CGRectMake(_userImgView.right+10*PERCEN,(WIDTH_5S ?  20*PERCEN :  28*PERCEN), (ISIPAD?(SCREEN_WIDTH-460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.)-_userImgView.right-10*PERCEN):(SCREEN_WIDTH-_userImgView.right-80*PERCEN)), 20);

    float musicWidth = [_musicTypeLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_musicTypeLb.font} context:NULL].size.width+6;
    _musicTypeLb.frame = CGRectMake(_nickNameLb.left, _nickNameLb.bottom-4, musicWidth, _nickNameLb.height);
    
    _musicSizeLb.frame = CGRectMake(_musicTypeLb.right+14*PERCEN, _musicTypeLb.top, 200, _musicTypeLb.height);

    float musicHeight = [_contentLb.text boundingRectWithSize:CGSizeMake(_nickNameLb.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_contentLb.font} context:NULL].size.height+4;
    _contentLb.frame = CGRectMake(_musicTypeLb.left, _musicTypeLb.bottom-4, _nickNameLb.width, (_contentBt.selected ? musicHeight : 20));
    _contentLb.numberOfLines = (_contentBt.selected ? 0 : 1);
    _contentBt.frame = CGRectMake((ISIPAD?_bgView.width-460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):_bgView.width-70*PERCEN), _nickNameLb.bottom, 70*PERCEN, 50);
    
    _lineView.frame = CGRectMake(_contentLb.left, _contentLb.bottom+10*PERCEN, (ISIPAD?_contentBt.right-_contentLb.left:_bgView.width-_contentLb.left), 1);
    
    _btView.frame = CGRectMake(0, _lineView.bottom+10*PERCEN, _bgView.width, 90*PERCEN);
  
    UIButton *playBt = (UIButton *)[self viewWithTag:1000];
    UIButton *downBt = (UIButton *)[self viewWithTag:1001];
    UIButton *moreBt = (UIButton *)[self viewWithTag:1002];
    playBt.frame = CGRectMake(_userImgView.left, 10*PERCEN, (ISIPAD?360*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):(ISPortrait?220*PERCEN:(_bgView.width-_userImgView.left*4)/3.)), 70*PERCEN);
    downBt.frame = CGRectMake(playBt.right+(ISIPAD?44*PERCEN:20*PERCEN), playBt.top, playBt.width, 70*PERCEN);
    moreBt.frame = CGRectMake(downBt.right+(ISIPAD?44*PERCEN:20*PERCEN), playBt.top, playBt.width, 70*PERCEN);

    _contentView.frame = CGRectMake(0, 0, _contentView.width, 255*PERCEN+[self getHeightForNewUI]);
    _bgView.frame = CGRectMake(0, 0, _bgView.width, _contentView.height+20*PERCEN);
}

@end
